<template>
  <div style="padding: 10px">
    <!-- 功能区域 -->
    <div style="margin: 10px 0">
      <el-button type="primary" @click="add">新增</el-button>
    </div>

    <!-- 搜索区域 -->
    <div style="margin: 10px 0">
      <el-input
        v-model="search"
        style="width: 20%"
        placeholder="请输入内容"
        clearable
      ></el-input>
      <el-button type="primary" style="margin-left: 5px" @click="load"
        >查询</el-button
      >
    </div>

    <!-- 表格区域 -->
    <div>
      <el-table :data="tableData" border stripe style="width: 100%">
        <el-table-column prop="id" label="ID" sortable />
        <el-table-column prop="username" label="用户名" />
        <el-table-column prop="beiwang" label="备忘录" />
        <el-table-column fixed="right" label="操作" width="150px">
          <template #default="scope">
            <el-popconfirm
              title="确定删除吗？"
              @confirm="handleDelete(scope.row.id)"
            >
              <template #reference>
                <el-button
                  type="danger"
                  size="small"
                  style="
                    border: 1px solid gray;
                    border-radius: 3px;
                    padding: 5px 10px;
                    background-color: #fd7a68;
                    color: white;
                  "
                >
                  删除
                </el-button>
              </template>
            </el-popconfirm>
            <el-button
              size="small"
              style="
                border: 1px solid gray;
                border-radius: 3px;
                padding: 5px 10px;
                color: #7a828f;
              "
              @click="handleEdit(scope.row)"
            >
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 添加or修改框 -->
      <div style="margin: 10px 0">
        <el-pagination
          style="margin-top: 10px"
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[5, 10, 20]"
          :small="small"
          :disabled="disabled"
          :background="background"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
        <el-dialog
          v-model="dialogVisible"
          title="新增用户"
          width="500"
          :before-close="handleClose"
        >
          <el-form :model="form" label-width="120px">
            <el-form-item label="用户名">
              <el-input v-model="form.username" style="width: 80%" />
            </el-form-item>
            <el-form-item label="备忘录">
              <el-input v-model="form.beiwang" style="width: 80%" />
            </el-form-item>
          </el-form>
          <template #footer>
            <div class="dialog-footer">
              <el-button @click="dialogVisible = false">取消</el-button>
              <el-button type="primary" @click="save"> 确定 </el-button>
            </div>
          </template>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "userUI",
  components: {},
  data() {
    return {
      form: {},
      dialogVisible: false,
      search: "",
      total: 0,
      pageSize: 5,
      currentPage: 1,
      tableData: [],
    };
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      // 刷新数据
      // 前端调试使用↓
      this.currentPage = 1;
      this.pageSize = 5;
      this.search = "";
      this.tableData = [
        {
          id: 1,
          username: "吉伊卡哇",
          nickName: "乌萨奇",
          beiwang: "asdjfkjahdkf",
        },
        {
          id: 2,
          username: "吉伊卡哇",
          nickName: "小八",
          beiwang: "ajfhkajdfn",
        },
      ];

      // 向后端发送请求
      // request
      //   .get("/user", {
      //     params: {
      //       pageNum: this.currentPage,
      //       pageSize: this.pageSize,
      //       search: this.search,
      //     },
      //   })
      //   .then((res) => {
      //     console.log(res);
      //     this.tableData = res.data.records;
      //     this.total = res.data.total;
      //   });
    },
    save() {
      // 保存填写内容

      // 前端调试使用↓
      // console.log(this.form);
      // this.tableData.push(this.form);
      // this.dialogVisible = false; // 关闭弹窗

      // 向后端发送form信息
      if (this.form.id) {
        // 更新
        request.put("/user", this.form).then((res) => {
          console.log(res);
          if (res.code === "0") {
            this.$message({
              type: "success",
              message: "更新成功",
            });
            this.load(); // 刷新数据
            this.dialogVisible = false; // 关闭弹窗
          } else {
            this.$message({
              type: "error",
              message: res.msg,
            });
          }
        });
      } else {
        // 新增
        request.post("/user", this.form).then((res) => {
          console.log(res);
          if (res.code === "0") {
            this.$message({
              type: "success",
              message: "新增成功",
            });
            this.load(); // 刷新数据
            this.dialogVisible = false; // 关闭弹窗
          } else {
            this.$message({
              type: "error",
              message: res.msg,
            });
          }
        });
      }
    },
    add() {
      // 新增
      this.form = {};
      this.dialogVisible = true;
    },
    handleDelete(id) {
      // 删除
      console.log(id);
      // 向后端发送请求
      request.delete("/user/" + id).then((res) => {
        console.log(res);
        if (res.code === "0") {
          this.$message({
            type: "success",
            message: "删除成功",
          });
          this.load(); // 刷新数据
        } else {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
      });
    },
    handleEdit(row) {
      // 编辑
      this.form = JSON.parse(JSON.stringify(row));
      console.log(this.form);
      this.dialogVisible = true;
    },
    handleSizeChange(pageSize) {
      // 改变当前每页的个数触发
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      // 改变当前页码触发
      this.currentPage = pageNum;
      this.load();
    },
  },
};
</script>
